﻿package com.firewolf.shiro.authentication.main;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Test;

/**
 * @Description: 
  	配置认证策略：
  	
 * @Author: 刘兴
 * @Date : 2016年7月20日
 * @package : com.firewolf.shiro.authenticationstrategy.main
 * @Java_Version : 1.7
 * @Version : 2016年7月20日下午3:25:27
 */
public class Driver {
	
	private void login(String confFile,String userName,String pwd){
		
		Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory(confFile);
		
		org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
		
		SecurityUtils.setSecurityManager(securityManager);
		
		Subject subject = SecurityUtils.getSubject();
		
		UsernamePasswordToken token = new UsernamePasswordToken(userName, pwd);
		
		subject.login(token);
		
	}
	
	/**
	 * @Description: 这种情况，两个Realm都能认证通过，所以整个能够通过
	 	AllSuccessfulStrategy：所有Realm验证成功才算成功，且返回所有Realm身份验证成功的认证信息，如果有一个失败就失败了。
	 * @Case：
	 */
	@Test
	public void testAllSuccessfulStrategyWithSuccess(){
		login("classpath:shiro-authenticator-all-success.ini","zhang","123");
        Subject subject = SecurityUtils.getSubject();
        //得到一个身份集合，其包含了Realm验证成功的身份信息
        PrincipalCollection principalCollection = subject.getPrincipals();
        System.out.println(principalCollection.asList().size());
	}
	
	@Test
	public void testAllSuccessfulStrategyWithFail(){
		
        try {
        	login("classpath:shiro-authenticator-all-fail.ini","zhang","123");
            Subject subject = SecurityUtils.getSubject();
            //得到一个身份集合，其包含了Realm验证成功的身份信息
            PrincipalCollection principalCollection = subject.getPrincipals();
            System.out.println(principalCollection.asList().size());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @Description: 只要有一个验证通过，就通过，
	 	FirstSuccessfulStrategy：只要有一个Realm验证成功即可，只返回第一个Realm身份验证成功的认证信息，其他的忽略；
	 * @Case：
	 */
	@Test
	public void testFirstSuccessfulStrategy(){
		login("classpath:shiro-authenticator-first-success.ini","zhang","123");
        Subject subject = SecurityUtils.getSubject();
        //得到一个身份集合，其包含了Realm验证成功的身份信息
        PrincipalCollection principalCollection = subject.getPrincipals();
        System.out.println(principalCollection.asList().size());
	}
	
	/**
	 * @Description:
		AtLeastOneSuccessfulStrategy：只要有一个Realm验证成功即可，和FirstSuccessfulStrategy不同，返回所有Realm身份验证成功的认证信息；
	 * @Case：
	 */
	@Test
	public void testAtLeastOneSuccessfulStrategy(){
		login("classpath:shiro-authenticator-atleastone.ini","zhang","123");
        Subject subject = SecurityUtils.getSubject();
        //得到一个身份集合，其包含了Realm验证成功的身份信息
        PrincipalCollection principalCollection = subject.getPrincipals();
        System.out.println(principalCollection.asList().size());
	}
	
	/**
	 * @Description:自定义策略，只允许一个策略认证通过
	 * @Case：
	 */
	@Test
	public void testSelfOnlyOneSucessfulStrategy(){
		login("classpath:shiro-authenticator-self-onlyone.ini","zhang","123");
        Subject subject = SecurityUtils.getSubject();
        //得到一个身份集合，其包含了Realm验证成功的身份信息
        PrincipalCollection principalCollection = subject.getPrincipals();
        System.out.println(principalCollection.asList().size());
	}
	
	/**
	 * @Description:自定义策略，至少有两个策略认证通过
	 * @Case：
	 */
	@Test
	public void testAtLeastTwoSucessfulStrategy(){
		try{
			login("classpath:shiro-authenticator-self-atleasttwo.ini","zhang","123");
	        Subject subject = SecurityUtils.getSubject();
	        //得到一个身份集合，其包含了Realm验证成功的身份信息
	        PrincipalCollection principalCollection = subject.getPrincipals();
	        System.out.println(principalCollection.asList().size());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
